package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.DimensionAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.DimensionBean;
import com.wwwarehouse.taskcenter.bean.job_point.SavedDimensionResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointBackEvent;
import com.wwwarehouse.taskcenter.eventbus_event.TaskPointCardEvent;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentAmountFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentCombinationFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentItemChangeFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentPositionFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentQualityFragment;
import com.wwwarehouse.taskcenter.fragment.job_point.step2.parent.ChangeTypeParentUseRightFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_IS_FROM_CARD;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_IS_PARENT;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_JOB_POINT_TYPE;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_JOB_POINT_UKID;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_NAME;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_UKID;

/**
 * Created by Jackie on 2017/10/27
 * 子作业点维度列表
 */

public class ChildJobPointDimensionListFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private StateLayout mStateLayout;
    private TextView mJobPointTypeTxt;
    private GridView mGridView;
    private DimensionAdapter mAdapter;
    private StateButton mSaveBtn;

    private String mBusinessId;
    private boolean mIsParent;
    private boolean mIsFromCard;
    private String mJobPointUkid;
    private String mJobPointType;
    private String mWarehouseUkid;
    private String mWarehouseName;

    private boolean mIsDimensionSaved = false;  //是否有保存过的维度

    private List<DimensionBean> mDimensionList;

    private ChildJObPointDimensionListReceiver mReceiver;

    public static final String KEY_DIMENSION_DETAILS = "key_dimension_details";

    private static final int REQUEST_GET_SAVED_DIMENSION_INFO = 0;
    private static final int REQUEST_GET_SAVE_STEP = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_child_job_point_dimension_list, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mActivity.unregisterReceiver(mReceiver);

        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(BackListenerEvent backListenerEvent) {
        if (peekFragment() instanceof ChildJobPointDimensionListFragment && backListenerEvent.getMsg().equals("ChildJobPointDimensionListFragment")) {
            // 判断有没有保存过的维度
            if (mDimensionList != null && mDimensionList.size() > 0) {
                for (DimensionBean dimensionBean : mDimensionList) {
                    if (dimensionBean.isFinish()) {
                        mIsDimensionSaved = true;
                        break;
                    }
                }
            } else {

            }

            if (mIsDimensionSaved) {
                if (mIsFromCard) {
                    //直接从拼卡界面跳转过来的
                    popFragment();
                } else {
                    // 直接返回到拼卡界面
                    popFragment();
                    popFragment();
                }

                TaskPointBackEvent taskPointBackEvent = new TaskPointBackEvent(mJobPointType, true);
                EventBus.getDefault().post(taskPointBackEvent);
            } else {
                // 直接返回到选择作业点类型界面
                popFragment();
            }
        }
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);
        mStateLayout.showProgressView(false);

        mJobPointTypeTxt = findView(mRootView, R.id.tv_job_point_type);
        mGridView = findView(mRootView, R.id.gv_dimension);
        mSaveBtn = findView(mRootView, R.id.btn_save);

        EventBus.getDefault().register(this);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString(KEY_BUSINESS_ID);
            mIsParent = bundle.getBoolean(KEY_IS_PARENT);
            mIsFromCard = bundle.getBoolean(KEY_IS_FROM_CARD);
            mJobPointUkid = bundle.getString(KEY_JOB_POINT_UKID);
            mJobPointType = bundle.getString(KEY_JOB_POINT_TYPE);
            mWarehouseUkid = bundle.getString(KEY_WAREHOUSE_UKID);
            mWarehouseName = bundle.getString(KEY_WAREHOUSE_NAME);

            mJobPointTypeTxt.setText(mJobPointType);
        }
    }

    private void initEvent() {
        mSaveBtn.setOnClickListener(this);

        mReceiver = new ChildJObPointDimensionListReceiver();
        mActivity.registerReceiver(mReceiver, new IntentFilter("ChangeTypeDimension"));
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChildJobPointDimensionListFragment) {
            mActivity.setTitle(R.string.choose_job_point_type);
        }
    }

    @Override
    public void requestDatas() {
        //查询已保存维度信息
        getSavedDimensionInfo();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_save) {
            saveStep();
        }
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {

        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_SAVED_DIMENSION_INFO:
                    if ("0".equals(commonClass.getCode())) {
                        SavedDimensionResponseBean savedDimensionResponseBean = JSON.parseObject(commonClass.getData().toString(), SavedDimensionResponseBean.class);
                        mDimensionList = buildDimensionList(savedDimensionResponseBean);

                        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                                int itemHeight = (mGridView.getHeight() - mGridView.getVerticalSpacing() * 2) / 3;

                                mAdapter = new DimensionAdapter(mActivity, mDimensionList, itemHeight);
                                mGridView.setAdapter(mAdapter);
                            }
                        });

                        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                DimensionBean dimensionBean = mDimensionList.get(position);

                                goDimension(dimensionBean);
                            }
                        });

                        //检查按钮是否点击
                        checkSaveBtn();
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);

                                getSavedDimensionInfo();
                            }
                        });
                    }
                    break;
                case REQUEST_GET_SAVE_STEP:
                    if ("0".equals(commonClass.getCode())) {
                        TaskPointCardEvent taskPointCardEvent = new TaskPointCardEvent();
                        taskPointCardEvent.setFinish2(true);

                        EventBus.getDefault().post(taskPointCardEvent);

                        if (mIsFromCard) {
                            //直接从拼卡界面跳转过来的
                            popFragment();
                        } else {
                            //从选择作业点类型界面跳转过来的
                            popFragment();
                            popFragment();
                        }
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);

                    getSavedDimensionInfo();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    private void getSavedDimensionInfo() {
        Map map = new HashMap();

        map.put("jobPointUkid", mJobPointUkid);

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_SAVED_DIMENSION_INFO, map, mOnResponseListener, REQUEST_GET_SAVED_DIMENSION_INFO);
    }

    private void saveStep() {
        Map map = new HashMap();

        map.put("jobPointUKid", mJobPointUkid);
        map.put("step", "2");

        NoHttpUtils.httpPost(TaskCenterConstant.URL_JOBPOINT_SAVESTEP, map, mOnResponseListener, REQUEST_GET_SAVE_STEP);
    }

    /**
     * 构造本地维度数据
     * @param savedDimensionResponseBean savedDimensionResponseBean
     * @return
     */
    private List<DimensionBean> buildDimensionList(SavedDimensionResponseBean savedDimensionResponseBean) {
        List<DimensionBean> dimensionList = new ArrayList<>();

        //组合
        if ("1".equals(savedDimensionResponseBean.getCombination())) {        //需要设置组合维度
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_combination);
            dimensionBean.setName("组合");

            if (savedDimensionResponseBean.getCombinationValue() != null) {
                //有保存过的组合维度
                dimensionBean.setFinish(true);

                //构造数据，用来回显
                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getCombinationValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.CombinationValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getCombinationValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getCombinationValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.CombinationValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getCombinationValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                //没有保存过的组合维度
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //位置
        if ("1".equals(savedDimensionResponseBean.getPosition())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_position);
            dimensionBean.setName("位置");

            if (savedDimensionResponseBean.getPositionValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getPositionValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.PositionValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getPositionValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getPositionValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.PositionValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getPositionValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //数量
        if ("1".equals(savedDimensionResponseBean.getAmount())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_amount);
            dimensionBean.setName("数量");

            if (savedDimensionResponseBean.getAmountValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getAmountValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.AmountValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getAmountValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getAmountValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.AmountValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getAmountValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //品质
        if ("1".equals(savedDimensionResponseBean.getQuality())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_quality);
            dimensionBean.setName("品质");

            if (savedDimensionResponseBean.getQualityValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getQualityValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.QualityValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getQualityValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getQualityValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.QualityValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getQualityValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //使用权
        if ("1".equals(savedDimensionResponseBean.getUseright())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_useright);
            dimensionBean.setName("使用权");

            if (savedDimensionResponseBean.getUserightValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getUserightValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.UserightValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getUserightValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getUserightValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.UserightValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getUserightValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //所有权
        if ("1".equals(savedDimensionResponseBean.getOwnership())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_ownership);
            dimensionBean.setName("所有权");

            if (savedDimensionResponseBean.getOwnershipValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getOwnershipValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.OwnershipValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getOwnershipValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getOwnershipValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.OwnershipValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getOwnershipValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //处置权
        if ("1".equals(savedDimensionResponseBean.getDisposition())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_disposition);
            dimensionBean.setName("处置权");

            if (savedDimensionResponseBean.getDispositionValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getDispositionValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.DispositionValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getDispositionValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getDispositionValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.DispositionValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getDispositionValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //委托权
        if ("1".equals(savedDimensionResponseBean.getEntrust())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_entrust);
            dimensionBean.setName("委托权");

            if (savedDimensionResponseBean.getEntrustValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getEntrustValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.EntrustValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getEntrustValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getEntrustValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.EntrustValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getEntrustValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        //物的转换
        if ("1".equals(savedDimensionResponseBean.getItemChange())) {
            DimensionBean dimensionBean = new DimensionBean();
            dimensionBean.setResId(R.drawable.dimension_item_change);
            dimensionBean.setName("物的转换");

            if (savedDimensionResponseBean.getItemChangeValue() != null) {
                dimensionBean.setFinish(true);

                DimensionBean.StartBean startBean = new DimensionBean.StartBean();
                startBean.setType(savedDimensionResponseBean.getItemChangeValue().getStart().getType());

                List<DimensionBean.StartBean.ListBean> startList = new ArrayList<>();
                for (SavedDimensionResponseBean.ItemChangeValueBean.StartBean.ListBean listBean : savedDimensionResponseBean.getItemChangeValue().getStart().getList()) {
                    DimensionBean.StartBean.ListBean bean = new DimensionBean.StartBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    startList.add(bean);
                }

                startBean.setList(startList);
                dimensionBean.setStart(startBean);

                DimensionBean.EndBean endBean = new DimensionBean.EndBean();
                endBean.setType(savedDimensionResponseBean.getItemChangeValue().getEnd().getType());

                List<DimensionBean.EndBean.ListBean> endList = new ArrayList<>();
                for (SavedDimensionResponseBean.ItemChangeValueBean.EndBean.ListBean listBean : savedDimensionResponseBean.getItemChangeValue().getEnd().getList()) {
                    DimensionBean.EndBean.ListBean bean = new DimensionBean.EndBean.ListBean();

                    bean.setKey(listBean.getKey());
                    bean.setValue(listBean.getValue());

                    endList.add(bean);
                }

                endBean.setList(endList);
                dimensionBean.setEnd(endBean);
            } else {
                dimensionBean.setFinish(false);
            }

            dimensionList.add(dimensionBean);
        }

        return dimensionList;
    }

    private void goDimension(DimensionBean dimensionBean) {
        if ("组合".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentCombinationFragment changeTypeParentCombinationFragment = new ChangeTypeParentCombinationFragment();
                changeTypeParentCombinationFragment.setArguments(bundle);
                pushFragment(changeTypeParentCombinationFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putString(KEY_WAREHOUSE_UKID, mWarehouseUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildCombinationFragment changeTypeChildCombinationFragment = new ChangeTypeChildCombinationFragment();
                changeTypeChildCombinationFragment.setArguments(bundle);
                pushFragment(changeTypeChildCombinationFragment);
            }
        } else if ("位置".equals(dimensionBean.getName())) {
            if (mIsParent) {
                //父作业点
                Bundle bundle = new Bundle();
                bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putString(KEY_WAREHOUSE_UKID, mWarehouseUkid);
                bundle.putString(KEY_WAREHOUSE_NAME, mWarehouseName);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentPositionFragment changeTypeParentPositionFragment = new ChangeTypeParentPositionFragment();
                changeTypeParentPositionFragment.setArguments(bundle);
                pushFragment(changeTypeParentPositionFragment);
            } else {
                //子作业点
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putString(KEY_WAREHOUSE_UKID, mWarehouseUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildPositionFragment changeTypeChildPositionFragment = new ChangeTypeChildPositionFragment();
                changeTypeChildPositionFragment.setArguments(bundle);
                pushFragment(changeTypeChildPositionFragment);
            }
        } else if ("数量".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentAmountFragment changeTypeParentAmountFragment = new ChangeTypeParentAmountFragment();
                changeTypeParentAmountFragment.setArguments(bundle);
                pushFragment(changeTypeParentAmountFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildAmountFragment changeTypeChildAmountFragment = new ChangeTypeChildAmountFragment();
                changeTypeChildAmountFragment.setArguments(bundle);
                pushFragment(changeTypeChildAmountFragment);
            }
        } else if ("品质".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentQualityFragment changeTypeParentQualityFragment = new ChangeTypeParentQualityFragment();
                changeTypeParentQualityFragment.setArguments(bundle);
                pushFragment(changeTypeParentQualityFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildQualityFragment changeTypeChildQualityFragment = new ChangeTypeChildQualityFragment();
                changeTypeChildQualityFragment.setArguments(bundle);
                pushFragment(changeTypeChildQualityFragment);
            }
        } else if ("使用权".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "useright");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentUseRightFragment changeTypeParentUseRightFragment = new ChangeTypeParentUseRightFragment();
                changeTypeParentUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeParentUseRightFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("type", "useright");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildUseRightFragment changeTypeChildUseRightFragment = new ChangeTypeChildUseRightFragment();
                changeTypeChildUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeChildUseRightFragment);
            }
        } else if ("所有权".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "ownership");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentUseRightFragment changeTypeParentUseRightFragment = new ChangeTypeParentUseRightFragment();
                changeTypeParentUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeParentUseRightFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("type", "ownership");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildUseRightFragment changeTypeChildUseRightFragment = new ChangeTypeChildUseRightFragment();
                changeTypeChildUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeChildUseRightFragment);
            }
        } else if ("处置权".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "disposition");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentUseRightFragment changeTypeParentUseRightFragment = new ChangeTypeParentUseRightFragment();
                changeTypeParentUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeParentUseRightFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("type", "disposition");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildUseRightFragment changeTypeChildUseRightFragment = new ChangeTypeChildUseRightFragment();
                changeTypeChildUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeChildUseRightFragment);
            }
        } else if ("委托权".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "entrust");
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentUseRightFragment changeTypeParentUseRightFragment = new ChangeTypeParentUseRightFragment();
                changeTypeParentUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeParentUseRightFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("type", "entrust");
                bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildUseRightFragment changeTypeChildUseRightFragment = new ChangeTypeChildUseRightFragment();
                changeTypeChildUseRightFragment.setArguments(bundle);
                pushFragment(changeTypeChildUseRightFragment);
            }
        } else if ("物的转换".equals(dimensionBean.getName())) {
            if (mIsParent) {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeParentItemChangeFragment changeTypeParentItemChangeFragment = new ChangeTypeParentItemChangeFragment();
                changeTypeParentItemChangeFragment.setArguments(bundle);
                pushFragment(changeTypeParentItemChangeFragment);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                bundle.putSerializable(KEY_DIMENSION_DETAILS, dimensionBean);

                ChangeTypeChildItemChangeFragment changeTypeChildItemChangeFragment = new ChangeTypeChildItemChangeFragment();
                changeTypeChildItemChangeFragment.setArguments(bundle);
                pushFragment(changeTypeChildItemChangeFragment);
            }
        }
    }

    private void checkSaveBtn() {
        if (mDimensionList != null && mDimensionList.size() > 0) {
            for (DimensionBean dimensionBean : mDimensionList) {
                if (!dimensionBean.isFinish()) {
                    mSaveBtn.setEnabled(false);
                    return;
                }
            }

            mSaveBtn.setEnabled(true);
        } else {
            mSaveBtn.setEnabled(false);
        }
    }

    private class ChildJObPointDimensionListReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if ("ChangeTypeDimension".equals(action)) {
                mStateLayout.showProgressView(false);

                //刷新界面
                getSavedDimensionInfo();
            }
        }
    }
 }
